package edu.fsu.cs.cis5930;

import java.util.List;
import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;

import android.provider.ContactsContract;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.RawContacts.Data;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;


import android.content.ContentResolver;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;

public class BusinessContentProvider {
	Context context;
	
	public BusinessContentProvider(Context cont) {
		context = cont;
	}
	
	public List<BusinessCard> getAllCards() {
		Uri uri = ContactsContract.Contacts.CONTENT_URI;
		String[] projection = new String[] {
				ContactsContract.Contacts._ID,
				StructuredName.GIVEN_NAME,
				StructuredName.FAMILY_NAME,
				Email.DATA,
				Phone.NUMBER,
				RawContacts.ACCOUNT_TYPE
				
		};
		
		Cursor cursor = context.getContentResolver().query(uri, projection, "account_type = BusinessCardExpress", null, null);
		
		cursor.moveToFirst();
		for(int i = 0; i < cursor.getCount(); i++)
		{
			Log.i("BusinessCardCP", cursor.getString(1));
			cursor.moveToNext();
		}
		cursor.close();	
		return null;
	}
	

	public BusinessCard getMyCard() {
		return null;
	}
	
	public void saveCard(BusinessCard card) {
		String first_name = card.first_name;
		String last_name = card.last_name;
		String phone_number = card.phone_number;
		String email = card.email;
		
		String accountType = "BusinessCardExpress";
		String accountName = phone_number;
		
		ContentResolver resolver = context.getContentResolver();
		
		ContentValues values = new ContentValues();
		values.put(RawContacts.ACCOUNT_NAME, accountName);
		values.put(RawContacts.ACCOUNT_TYPE, accountType);
		Uri rawContactUri = resolver.insert(RawContacts.CONTENT_URI, values);
		
		Uri dataUri = Uri.withAppendedPath(rawContactUri, Data.CONTENT_DIRECTORY);
		
		long rawContactId = ContentUris.parseId(rawContactUri);
		values.clear();
		values.put(Data.RAW_CONTACT_ID, rawContactId);
		values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		values.put(StructuredName.GIVEN_NAME, first_name);
		values.put(StructuredName.FAMILY_NAME, last_name);
	
		resolver.insert(dataUri, values);		
		
		values.clear();
		values.put(Data.RAW_CONTACT_ID, rawContactId);
		values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
		values.put(Email.DATA, email);
		resolver.insert(dataUri, values);
		
		values.clear();
		values.put(Data.RAW_CONTACT_ID, rawContactId);
		values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
		values.put(Phone.NUMBER, phone_number);
		resolver.insert(dataUri, values);								
		
	}
	
	public void saveMyCard(BusinessCard card) {
		
	}

}
